package com.xing.bigdata;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.HashSet;
import java.util.Set;
import java.util.zip.CRC32;

/**
 * Created by YLZX-Z0107 on 2014/11/4.
 */
public class CreateUserSql {
    private static CRC32 crc32 = new CRC32();
    private static int page = 7;

    public static void main(String[] args) {
        CreateUserSql createUserSql = new CreateUserSql();
        createUserSql.createSql();
    }

    public void createSql() {
        Set<Long> uids = getUidFromFile();
        FileWriter fw;
        long start = System.currentTimeMillis();
        System.out.println("开始生成sql文件: " + start + " seuid: " + uids.size());
        long init = 51801378;
        String sql0 = "insert into user0 values ";
        String sql1 = "insert into user1 values ";
        String sql2 = "insert into user2 values ";
        String sql3 = "insert into user3 values ";
        String sql4 = "insert into user4 values ";
        String sql5 = "insert into user5 values ";
        String sql6 = "insert into user6 values ";
        String sql7 = "insert into user7 values ";
        String sql8 = "insert into user8 values ";
        String sql9 = "insert into user9 values ";
        int s0 = 1;
        int s1 = 1;
        int s2 = 1;
        int s3 = 1;
        int s4 = 1;
        int s5 = 1;
        int s6 = 1;
        int s7 = 1;
        int s8 = 1;
        int s9 = 1;
        try {
            fw = new FileWriter("D:\\test\\add"+page+".txt");
            for (Long uid : uids) {
                long wid = 8000000000l + (init * 10000l) + (int) (Math.random() * 10000);
                int tableNum = getCRC(wid);
                switch (tableNum) {
                    case 0:
                        sql0 += "(" + wid + ",1,0," + uid + ",'','',UNIX_TIMESTAMP()),";
                        if (s0 % 500 == 0) {
                            fw.write(sql0.substring(0, sql0.length() - 1) + ";\r\n");
                            sql0 = "insert into user0 values ";
                            s0 = 1;
                            fw.flush();
                        } else {
                            s0++;
                            init++;
                        }
                        break;
                    case 1:
                        sql1 += "(" + wid + ",1,0," + uid + ",'','',UNIX_TIMESTAMP()),";
                        if (s1 % 500 == 0) {
                            fw.write(sql1.substring(0, sql1.length() - 1) + ";\r\n");
                            sql1 = "insert into user1 values ";
                            s1 = 1;
                            fw.flush();
                        } else {
                            s1++;
                            init++;
                        }
                        break;
                    case 2:
                        sql2 += "(" + wid + ",1,0," + uid + ",'','',UNIX_TIMESTAMP()),";
                        if (s2 % 500 == 0) {
                            fw.write(sql2.substring(0, sql2.length() - 1) + ";\r\n");
                            sql2 = "insert into user2 values ";
                            s2 = 1;
                            fw.flush();
                        } else {
                            s2++;
                            init++;
                        }
                        break;
                    case 3:
                        sql3 += "(" + wid + ",1,0," + uid + ",'','',UNIX_TIMESTAMP()),";
                        if (s3 % 500 == 0) {
                            fw.write(sql3.substring(0, sql3.length() - 1) + ";\r\n");
                            sql3 = "insert into user3 values ";
                            s3 = 1;
                            fw.flush();
                        } else {
                            s3++;
                            init++;
                        }
                        break;
                    case 4:
                        sql4 += "(" + wid + ",1,0," + uid + ",'','',UNIX_TIMESTAMP()),";
                        if (s4 % 500 == 0) {
                            fw.write(sql4.substring(0, sql4.length() - 1) + ";\r\n");
                            sql4 = "insert into user4 values ";
                            s4 = 1;
                            fw.flush();
                        } else {
                            s4++;
                            init++;
                        }
                        break;
                    case 5:
                        sql5 += "(" + wid + ",1,0," + uid + ",'','',UNIX_TIMESTAMP()),";
                        if (s5 % 500 == 0) {
                            fw.write(sql5.substring(0, sql5.length() - 1) + ";\r\n");
                            sql5 = "insert into user5 values ";
                            s5 = 1;
                            fw.flush();
                        } else {
                            s5++;
                            init++;
                        }
                        break;
                    case 6:
                        sql6 += "(" + wid + ",1,0," + uid + ",'','',UNIX_TIMESTAMP()),";
                        if (s6 % 500 == 0) {
                            fw.write(sql6.substring(0, sql6.length() - 1) + ";\r\n");
                            sql6 = "insert into user6 values ";
                            s6 = 1;
                            fw.flush();
                        } else {
                            s6++;
                            init++;
                        }
                        break;
                    case 7:
                        sql7 += "(" + wid + ",1,0," + uid + ",'','',UNIX_TIMESTAMP()),";
                        if (s7 % 500 == 0) {
                            fw.write(sql7.substring(0, sql7.length() - 1) + ";\r\n");
                            sql7 = "insert into user7 values ";
                            s7 = 1;
                            fw.flush();
                        } else {
                            s7++;
                            init++;
                        }
                        break;
                    case 8:
                        sql8 += "(" + wid + ",1,0," + uid + ",'','',UNIX_TIMESTAMP()),";
                        if (s8 % 500 == 0) {
                            fw.write(sql8.substring(0, sql8.length() - 1) + ";\r\n");
                            sql8 = "insert into user8 values ";
                            s8 = 1;
                            fw.flush();
                        } else {
                            s8++;
                            init++;
                        }
                        break;
                    case 9:
                        sql9 += "(" + wid + ",1,0," + uid + ",'','',UNIX_TIMESTAMP()),";
                        if (s9 % 500 == 0) {
                            fw.write(sql9.substring(0, sql9.length() - 1) + ";\r\n");
                            sql9 = "insert into user9 values ";
                            s9 = 1;
                            fw.flush();
                        } else {
                            s9++;
                            init++;
                        }
                        break;
                }
            }
            System.out.println(init);
            if (sql0.length() > 25) {
                fw.write(sql0.substring(0, sql0.length() - 1) + ";\r\n");
            }
            if (sql1.length() > 25) {
                fw.write(sql1.substring(0, sql1.length() - 1) + ";\r\n");
            }
            if (sql2.length() > 25) {
                fw.write(sql2.substring(0, sql2.length() - 1) + ";\r\n");
            }
            if (sql3.length() > 25) {
                fw.write(sql3.substring(0, sql3.length() - 1) + ";\r\n");
            }
            if (sql4.length() > 25) {
                fw.write(sql4.substring(0, sql4.length() - 1) + ";\r\n");
            }
            if (sql5.length() > 25) {
                fw.write(sql5.substring(0, sql5.length() - 1) + ";\r\n");
            }
            if (sql6.length() > 25) {
                fw.write(sql6.substring(0, sql6.length() - 1) + ";\r\n");
            }
            if (sql7.length() > 25) {
                fw.write(sql7.substring(0, sql7.length() - 1) + ";\r\n");
            }
            if (sql8.length() > 25) {
                fw.write(sql8.substring(0, sql8.length() - 1) + ";\r\n");
            }
            if (sql9.length() > 25) {
                fw.write(sql9.substring(0, sql9.length() - 1) + ";\r\n");
            }
            fw.flush();
            fw.close();
            System.out.println("生成sql完毕: " + (System.currentTimeMillis() - start));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private int getCRC(long wid) {
        crc32.update((wid + "").getBytes());
        long result = crc32.getValue();
        int table = (int) result % 10;
        return Math.abs(table);
    }

    private Set<Long> getUidFromFile() {
        try {
            final int BUFFER_SIZE = 15 * 1024 * 1024;// 缓冲区大小
            Set<Long> setUid = new HashSet<Long>();
            File file = new File("D:\\test\\user_level1.txt");
            MappedByteBuffer inputBuffer = new RandomAccessFile(file, "r").getChannel().map(FileChannel.MapMode.READ_ONLY, file.length() * page / 8, file.length() / 8);
            byte[] dst = new byte[BUFFER_SIZE];
            long start = System.currentTimeMillis();
            for (int offset = 0; offset < inputBuffer.capacity(); offset += BUFFER_SIZE) {
                if (inputBuffer.capacity() - offset >= BUFFER_SIZE) {
                    for (int i = 0; i < BUFFER_SIZE; i++)
                        dst[i] = inputBuffer.get(offset + i);
                } else {
                    for (int i = 0; i < inputBuffer.capacity() - offset; i++)
                        dst[i] = inputBuffer.get(offset + i);
                }
                String line = new String(dst);
                String[] lines = line.split("\n");
                for (int i = 1; i < lines.length - 1; i++) {
                    if (lines[i].length() > 0) {
                        setUid.add(Long.parseLong(lines[i].trim()));
                    }
                }
            }
            long end = System.currentTimeMillis();
            System.out.println("读取文件文件内容加初始化线程安全容器花费：" + (end - start) + "毫秒 all setUid: " + setUid.size());
            return setUid;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
